package com.ellecity06.common.helper

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent

/**
 * @author ellecity06
 * @time 2023/4/15 9:49
 * @des 登录帮助类
 */
class LoginHelper {

    fun initialize(clazz: Class<out Activity>, block: (() -> Unit)? = null) {
        loginClazz = clazz
        logoutBlock = block
    }

    fun logout(context: Context) {
        if (loginClazz == null) throw RuntimeException("请先初始化LoginHelper")
        logoutBlock?.invoke()
        ActivityManagerHelper.instance.finshAll()
        context.startActivity(Intent(context, loginClazz).apply { addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) })
    }

    companion object {
        private var loginClazz: Class<out Activity>? = null
        private var logoutBlock: (() -> Unit)? = null
        val instance = Holder.holder
    }

    object Holder {
        @SuppressLint("StaticFieldLeak")
        val holder = LoginHelper()
    }
}